<?php
/*
	[BuyPg] (C) 2010-2011 Buypg
	$Id: UsercpAction.class.php 2011-5-11 9:28:37 wufujun $
*/

if (!defined('THINK_PATH')) {
	exit('Access Denied');
}

class UsercpAction extends BpAction{
	
	public function index(){
		global $_SGLOBAL;
		$op = empty($_GET['op']) ? '' : $_GET['op'];
		//判断用户
		if(($mserr = checkstatus('user'))){
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD'], $jumpUrl);
				$this->assign('jumpUrl' , __APP__."/User/login");
			}
			$this->error($mserr['msg']);
		}
		$DbUser = D("User");
		$arrUser = $DbUser->where("user_id = ".$_SGLOBAL['G_uid'])->find();
		
		//更新用户信息
		if($op === 'update'){
			if($DbUser->create()){
				if(false !== $DbUser->save()) {
					$this->assign('jumpUrl',__URL__);
					$this->success("您已经成功更新信息");
				}else{
					$this->assign('jumpUrl',__URL__);
					$this->error('更新失败');
				} 
			}else{
				$this->assign('jumpUrl',__URL__);
				$this->error($DbUser->getError());
			}
		}
		
		//模板输出
		$this->assign('arrUser' , $arrUser);
		$this->display();
	}
	
	//提交网站
	public function submitweb(){
		global $_SGLOBAL;
		$op = empty($_GET['op']) ? '' : $_GET['op'];
		//判断用户
		if(($mserr = checkstatus('user'))){
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD'], $jumpUrl);
				$this->assign('jumpUrl' , __APP__."/User/login");
			}
			$this->error($mserr['msg']);
		}
		//获取分类
		$arrCate = getCategory();
		if($op === 'insert'){
			$DbSite = D("NbSite");
			if($DbSite->create()){
				if($DbSite->add() !== FALSE){
					$this->assign('jumpUrl',__URL__."/submitweb");
					$this->success("您已经成功提交网站，请耐心等待管理员审核");
				}else{
					$this->assign('jumpUrl',__URL__."/submitweb");
					$this->error("数据提交失败");
				}
			}else{
				$this->assign('jumpUrl',__URL__."/submitweb");
				$this->error($DbSite->getError());
			}
			
		}
		//模板输出
		$this->assign('arrCate' , $arrCate);
		$this->display();
	}
	
	//修改密码
	public function resetpwd(){
		global $_SGLOBAL;
		$op = empty($_GET['op']) ? '' : $_GET['op'];
		//判断用户
		if(($mserr = checkstatus('user'))){
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD'], $jumpUrl);
				$this->assign('jumpUrl' , __APP__."/User/login");
			}
			$this->error($mserr['msg']);
		}
		$DbUser = D("User");
		$arrUser = $DbUser->where("user_id = ".$_SGLOBAL['G_uid'])->find();
		
		if($op === 'update'){
			$post = $_POST;
			$this->assign('jumpUrl',__URL__."/resetpwd");
			if($post['user_ans'] != $arrUser['user_ans']){
				$this->error("回答不正确");
			}
			$data = array(
				'user_password' => $post['user_password'],
				'repassword' => $post['repassword'],
				'user_id' => $post['user_id'],
				'__hash__' => $post['__hash__'],
			);
			//动态修改自动完成规则（重设密码时，密码加密）
			 $auto = array ( 
				array('user_password','sha1',2,'function'), // 对password字段在新增的时候使sha1函数处理
				array('user_lastdate','getTimeToStr',3,'function'),
			);

			$DbUser-> setProperty("_auto",$auto);

			if($DbUser->create($data)){
				if(false !== $DbUser->save()) {
					$this->success("您已经成功更新信息");
				}else{
					$this->error('更新失败');
				} 
			}else{
				$this->error($DbUser->getError());
			}
			
		}
		
		//输板输出
		$this->assign('arrUser' , $arrUser);
		$this->display();
	}
	
	//投诉管理
	public function usertousu(){
		global $_SGLOBAL;
		//判断用户
		if(($mserr = checkstatus('user'))){
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD'], $jumpUrl);
				$this->assign('jumpUrl' , __APP__."/User/login");
			}
			$this->error($mserr['msg']);
		}
		$DbTouSu = M("NbTousu");
		import("@.ORG.Page"); //导入分页类
		$count = $DbTouSu->where("user_id = ".$_SGLOBAL['G_uid']." and flag = 1")->count();    //计算总数
		$p = new Page ( $count, 20 );
		//$arrTouSu = $DbTouSu->where("user_id = ".$_SGLOBAL['G_uid']." and flag = 1")->limit($p->firstRow.','.$p->listRows)->order("tousu_dt DESC")->select();
		$arrTouSu = $DbTouSu->query("SELECT ts.* , s.site_name FROM `t_nb_tousu` ts LEFT JOIN `t_nb_site` s ON ts.site_id = s.site_id WHERE ts.user_id = ".$_SGLOBAL['G_uid']." and ts.flag = 1 ORDER BY ts.tousu_dt DESC LIMIT $p->firstRow , $p->listRows ");
		$p->setConfig('header','篇记录');
		$p->setConfig('prev','<');
		$p->setConfig('next','>');
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');
		$p->setConfig('theme', ' <span>%totalRow% %header% %nowPage%/%totalPage% 页 </span> %first% %upPage% %linkPage% %downPage% %end%');
		$page = $p->show(1);
		
		//模板输出
		$this->assign('arrTouSu' , $arrTouSu);
		$this->assign('page' , $page);
		$this->display();
	}
	
	//电子合同
	public function hetong(){
		global $_SGLOBAL;
		//判断用户
		if(($mserr = checkstatus('user'))){
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD'], $jumpUrl);
				$this->assign('jumpUrl' , __APP__."/User/login");
			}
			$this->error($mserr['msg']);
		}
		
		$DbHeTong = M('Hetong');
		import("@.ORG.Page"); //导入分页类
		$count = $DbHeTong->where("user_id = ".$_SGLOBAL['G_uid'])->count();    //计算总数
		$p = new Page ( $count, 20 );
		$arrHeTong = $DbHeTong->where("user_id = ".$_SGLOBAL['G_uid'])->limit($p->firstRow.','.$p->listRows)->order('hetong_dt DESC')->select();
		
		$p->setConfig('header','篇记录');
		$p->setConfig('prev','<');
		$p->setConfig('next','>');
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');
		$p->setConfig('theme', ' <span>%totalRow% %header% %nowPage%/%totalPage% 页 </span> %first% %upPage% %linkPage% %downPage% %end%');
		$page = $p->show(1);
		
		//模板输出
		$this->assign('arrHeTong' , $arrHeTong);
		$this->assign('page' , $page);
		$this->display();
		
	}
	
	//合同显示
	public function showhetong(){
		global $_SGLOBAL;
		//判断用户
		if (($mserr = checkstatus('user'))) {
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD'], $jumpUrl);
				$this->assign('jumpUrl' , __APP__."/User/login");
			}
			$this->error($mserr['msg']);
		}

		$htid = isset ($_GET['hetongid']) ? intval($_GET['hetongid']) : 0;
		$DbHetong = M('Hetong');
		$arrHetong = $DbHetong->where("hetong_id = $htid and user_id = ".$_SGLOBAL['G_uid'])->find();
		//判断合同是否存在
		if(!$arrHetong){
			$this->error('您查看的信息不存在或已经删除');
		}
		//获取合同产品
		$DbProduct = M('Product');
		$arrProduct = $DbProduct->where("hetong_id = $htid and user_id = ".$arrHetong['biz_id'])->select();
		//附件分类（所在文件夹）
		$attModel = 'hetongpdt';
		foreach ($arrProduct as $key => $value){
			if($value['product_img']){
				$arrProduct[$key]['product_img'] = __ROOT__  ."/attachment/".$value['product_img'];
				$arrProduct[$key]['product_img_m'] = __ROOT__  ."/attachment/".$value['product_img'].".m.jpg";
				$arrProduct[$key]['product_img_s'] = __ROOT__  ."/attachment/".$value['product_img'].".s.jpg";
			}
		}
		//模板输出
		$this->assign('arrHetong' , $arrHetong);
		$this->assign('arrProduct' , $arrProduct);
		$this->display();
	}
	
	//签订合同
	public function hetongok(){
		global $_SGLOBAL;
		//判断用户
		if (($mserr = checkstatus('user'))) {
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD'], $jumpUrl);
				$this->assign('jumpUrl' , __APP__."/User/login");
			}
			$this->error($mserr['msg']);
		}
		
		$htid = isset ($_GET['hetongid']) ? intval($_GET['hetongid']) : 0;
		$DbHetong = M('Hetong');
		$arrHetong = $DbHetong->where("hetong_id = $htid and user_id = ".$_SGLOBAL['G_uid'])->find();
		//判断合同是否存在
		if(!$arrHetong){
			$this->error('您查看的信息不存在或已经删除');
		}
		$DbUser = M('User');
		$arrUser = $DbUser->where("user_id = ".$_SGLOBAL['G_uid'])->find();
		if(empty($arrUser['user_phone']) || empty ($arrUser['user_address'])){
			$this->assign('jumpUrl',__URL__);
			$this->error('您的资料不完，请完善您的个人信息，再签订');
		}
		$data = array(
			'hetong_id' => $arrHetong['hetong_id'],
			'user_phone' => $arrUser['user_phone'],
			'user_address' => $arrUser['user_address'],
			'hetong_flag' => 1,
		);
		$DbHetong->data($data)->save();
		$this->assign('jumpUrl' , __URL__.'/hetong');
		$this->success('合同签订成功！');
	}

	//用户签到
	public function usersign(){
		global $_SGLOBAL;
		//跳转页面
		$jumpUrl =  U(APP_NAME."://Index/");
		$this->assign('jumpUrl' , $jumpUrl);
		
		//判断用户
		if(($mserr = checkstatus('user'))){
			if($mserr['flag'] == 1){
				checkrefer($_SERVER['REQUEST_METHOD'], $jumpUrl);
				$this->assign('jumpUrl' , __APP__."/User/login");
			}
			$this->error($mserr['msg']);
		}
		if($_SGLOBAL['user_sign']){
			$this->assign('jumpUrl' , __APP__."/index");
			$this->error('您今天已经完成签到！');
		}else{
			$credit = C('BUYPG_CREDIT');
			$data['signtime'] = $_SGLOBAL['timestamp'];
			$model = M('User');
			$model->setInc( 'credit','user_id = '.$_SGLOBAL['G_uid'],$credit);
			$model->where('user_id = '.$_SGLOBAL['G_uid'])->data($data)->save();
			$this->assign('jumpUrl' , __APP__."/index");
			$this->success('签到成功！');
		}
	}
}
?>
